<template>
  <div class="comments">
    <myheader title="精彩评论">
      <span
        slot="left"
        class="iconfont iconjiantou2"
        @click="$router.back()"
      ></span>
    </myheader>
    <div class="lists">
      <div class="item" v-for="(v, i) in commentList" :key="i">
        <div class="head">
          <img :src="v.user.head_img" alt />
          <div>
            <p>{{ v.user.nickname }}</p>
            <!-- <span>{{ v.create_date }}-自己搞定</span> -->
            <span>{{ $moment(v.create_date).fromNow() }}</span>
          </div>
          <span @click="replyComment(v)"> 回复</span>
        </div>
        <!-- 不是每个评论都会有parent,如果没有，则不生成评论块结构 -->
        <commentItem
          v-if="v.parent"
          :parent="v.parent"
          @replyComment="replyComment"
        ></commentItem>
        <div class="text">{{ v.content }}</div>
      </div>
    </div>
    <!-- //底部评论框 -->
    <commentFooter
      :post="article"
      @refresh="refresh"
      :obj="comment"
      @resetObj="comment = null"
    ></commentFooter>
  </div>
</template>

<script>
import myheader from "@/components/myheader.vue";
import commentFooter from "@/components/commentFooter.vue";
import commentItem from "@/components/commentItem.vue";
import { getArticleComments, getArticleById } from "@/apis/article.js";
import axios from "@/utils/myaxios.js";
export default {
  components: {
    myheader,
    commentItem,
    commentFooter,
  },
  data() {
    return {
      commentList: [],
      article: {},
      comment: {}, //父亲组件中单击恢复得时候，需要传递给子组件得评论数据对象
    };
  },
  methods: {
    //单机恢复
    replyComment(v) {
      // console.log("===", v);
      this.comment = v;
    },
    refresh() {
      this.init();
      //让用户看到评论：将页面滚动到最顶部
      window.scrollTo(0, 0);
    },
    async init() {
      let id = this.$route.params.id;
      let res = await getArticleComments(id);
      this.commentList = res.data.data.map((v) => {
        if (v.user.head_img) {
          v.user.head_img = axios.defaults.baseURL + v.user.head_img;
        } else {
          v.user.head_img =
            axios.defaults.baseURL + "/uploads/image/default.png";
        }
        return v;
      });
      //获取文章的详情
      let article = await getArticleById(id);
      this.article = article.data.data;
    },
  },
  async mounted() {
    this.init();
    // let id = this.$route.params.id;
    // let res = await getArticleComments(id);
    // console.log(res);
    // this.commentList = res.data.data.map((v) => {
    //   if (v.user.head_img) {
    //     v.user.head_img = axios.defaults.baseURL + v.user.head_img;
    //   } else {
    //     v.user.head_img = axios.defaults.baseURL + "/uploads/image/default.png";
    //   }
    //   return v;
    // });
    // //获取文章的详情
    // let article = await getArticleById(id);
    // this.article = article.data.data;
  },
};
</script>

<style lang='less' scoped>
.lists {
  border-top: 5px solid #ddd;
  padding: 0 15px;
  .item {
    padding: 10px 0;
    border-bottom: 1px solid #ccc;
    .head {
      display: flex;
      justify-content: space-between;
      align-items: center;
      > img {
        width: 50/360 * 100vw;
        height: 50/360 * 100vw;
        display: block;
        border-radius: 50%;
      }
      > div {
        flex: 1;
        display: flex;
        flex-direction: column;
        margin-left: 10px;
        > span {
          font-size: 12px;
          color: #999;
          line-height: 25px;
        }
      }
      > span {
        color: #999;
        font-size: 13px;
      }
    }
    .text {
      font-size: 14px;
      color: #333;
      padding: 20px 0 10px 0;
    }
  }
}
</style>